package com.xiaoxu.intranetweb.mybatisplusSzappdb.business.weightSheets;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.CProductsInfo;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.CWeightSheets;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ICProductsInfoService;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ICWeightSheetsService;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class WeightSheetsTimeImprove {
    @Autowired
    private ICProductsInfoService  cProductsInfoService;
    @Autowired
    private ICWeightSheetsService cWeightSheetsService;
    public void timeImprove() {
//        处理不需要进门时间的
//        timeImprove1();
//        timeImprove2();
//        timeImprove3();
//        timeImprove4();
//        timeImprove5();
//        timeImprove6();
//        timeImprove7();
    }
    //        处理不需要进门时间的
    public void timeImprove1() {
        List<String> pdtName = new ArrayList<>();
        QueryWrapper<CProductsInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("PDTINF_CLASS",3,4,0);
        List<CProductsInfo> productsInfoList = cProductsInfoService.list(queryWrapper);
        for (CProductsInfo cProductsInfo : productsInfoList) {
            pdtName.add(cProductsInfo.getPdtinfPdt());
        }
        QueryWrapper<CWeightSheets> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.isNotNull("WEISHT_ENTER_TM");
        queryWrapper1.in("WEISHT_PRODUCT", pdtName);
        queryWrapper1.eq("WEISHT_DELETED",0);
        queryWrapper1.eq("WEISHT_BACKED", 1);
        List<CWeightSheets> cWeightSheetsList = cWeightSheetsService.list(queryWrapper1);
//        然后把进厂时间全部置成空
        for (CWeightSheets cWeightSheets : cWeightSheetsList) {
            UpdateWrapper<CWeightSheets> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("ID", cWeightSheets.getId());
            updateWrapper.set("WEISHT_ENTER_TM", null);
            cWeightSheetsService.update(updateWrapper);
        }
    }
//    处理没有进门时间的，给进门时间按照WEISHT_EMPTY_TM往前38秒
    public void timeImprove2() {
        List<String> pdtName = new ArrayList<>();
        QueryWrapper<CProductsInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("PDTINF_CLASS", 1, 2);
        List<CProductsInfo> productsInfoList = cProductsInfoService.list(queryWrapper);
        for (CProductsInfo cProductsInfo : productsInfoList) {
            pdtName.add(cProductsInfo.getPdtinfPdt());
        }
        QueryWrapper<CWeightSheets> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.isNull("WEISHT_ENTER_TM");
        queryWrapper1.in("WEISHT_PRODUCT", pdtName);
        queryWrapper1.eq("WEISHT_DELETED",0);
        queryWrapper1.eq("WEISHT_BACKED", 1);
        queryWrapper1.notIn("WEISHT_SEL_ASHGATE_ID",timeImproveByAshgate2());
        List<CWeightSheets> cWeightSheetsList = cWeightSheetsService.list(queryWrapper1);
        for (CWeightSheets cWeightSheets : cWeightSheetsList) {
            UpdateWrapper<CWeightSheets> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("ID", cWeightSheets.getId());
            updateWrapper.set("WEISHT_ENTER_TM", cWeightSheets.getWeishtEmptyTm().minusSeconds(38));
            cWeightSheetsService.update(updateWrapper);
        }
    }
    //    处理没有出门时间的，给出门时间按照WEISHT_FULL_TM往后53秒
    public void timeImprove3() {

        QueryWrapper<CWeightSheets> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.isNull("WEISHT_OUT_TM");
        queryWrapper1.isNotNull("WEISHT_FULL_TM");
        queryWrapper1.eq("WEISHT_DELETED",0);
        queryWrapper1.eq("WEISHT_BACKED", 1);
        queryWrapper1.notIn("WEISHT_SEL_ASHGATE_ID",timeImproveByAshgate2());
        List<CWeightSheets> cWeightSheetsList = cWeightSheetsService.list(queryWrapper1);
            for (CWeightSheets cWeightSheets : cWeightSheetsList) {
                UpdateWrapper<CWeightSheets> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("ID", cWeightSheets.getId());
                updateWrapper.set("WEISHT_OUT_TM", cWeightSheets.getWeishtFullTm().plusSeconds(53));
                cWeightSheetsService.update(updateWrapper);
            }
    }
//    处理没有装载开始时间的，给装载开始时间按照WEISHT_EMPTY_TM往后183秒
    public void timeImprove4() {
        List<String> pdtName = new ArrayList<>();
        QueryWrapper<CProductsInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("PDTINF_CLASS", 1, 2);
        queryWrapper.eq("PDTINF_LOAD_CTRL",1);
        List<CProductsInfo> productsInfoList = cProductsInfoService.list(queryWrapper);
        for (CProductsInfo cProductsInfo : productsInfoList) {
            String pdt = cProductsInfo.getPdtinfPdt();
            if (!"管道灰".equals(pdt) && !"大袋灰".equals(pdt)) {
                pdtName.add(pdt);
            }
        }
        QueryWrapper<CWeightSheets> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.isNull("WEISHT_LOAD_TM");
        queryWrapper1.isNotNull("WEISHT_EMPTY_TM");
        queryWrapper1.eq("WEISHT_DELETED", 0);
        queryWrapper1.eq("WEISHT_BACKED", 1);
        queryWrapper1.in("WEISHT_PRODUCT", pdtName);
        queryWrapper1.notIn("WEISHT_SEL_ASHGATE_ID",timeImproveByAshgate());
        List<CWeightSheets> cWeightSheetsList = cWeightSheetsService.list(queryWrapper1);
        for (CWeightSheets cWeightSheets : cWeightSheetsList) {
            UpdateWrapper<CWeightSheets> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("ID", cWeightSheets.getId());
            updateWrapper.set("WEISHT_LOAD_TM", cWeightSheets.getWeishtEmptyTm().plusSeconds(183));
            cWeightSheetsService.update(updateWrapper);
        }
    }
//    处理没有装载结束时间的，给装载结束按照WEISHT_FULL_TM往前194秒
    public void timeImprove5() {
        List<String> pdtName = new ArrayList<>();
        QueryWrapper<CProductsInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("PDTINF_CLASS", 1, 2);
        queryWrapper.eq("PDTINF_LOAD_CTRL",1);
        List<CProductsInfo> productsInfoList = cProductsInfoService.list(queryWrapper);
        for (CProductsInfo cProductsInfo : productsInfoList) {
            String pdt = cProductsInfo.getPdtinfPdt();
            if (!"管道灰".equals(pdt) && !"大袋灰".equals(pdt)) {
                pdtName.add(pdt);
            }
        }
        QueryWrapper<CWeightSheets> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.isNull("WEISHT_LOAD_END_TM");
        queryWrapper1.isNotNull("WEISHT_FULL_TM");
        queryWrapper1.eq("WEISHT_DELETED", 0);
        queryWrapper1.eq("WEISHT_BACKED", 1);
        queryWrapper1.notIn("WEISHT_SEL_ASHGATE_ID",timeImproveByAshgate());
        queryWrapper1.in("WEISHT_PRODUCT", pdtName);
        List<CWeightSheets> cWeightSheetsList = cWeightSheetsService.list(queryWrapper1);
        for (CWeightSheets cWeightSheets : cWeightSheetsList) {
            UpdateWrapper<CWeightSheets> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("ID", cWeightSheets.getId());
            updateWrapper.set("WEISHT_LOAD_END_TM", cWeightSheets.getWeishtFullTm().minusSeconds(194));
            cWeightSheetsService.update(updateWrapper);
        }
    }

    //     处理不需要装载结束时间的
    public void timeImprove6() {
        List<String> pdtName = new ArrayList<>();
        QueryWrapper<CProductsInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("PDTINF_CLASS", 4);
        List<CProductsInfo> productsInfoList = cProductsInfoService.list(queryWrapper);
        for (CProductsInfo cProductsInfo : productsInfoList) {
            String pdt = cProductsInfo.getPdtinfPdt();
                pdtName.add(pdt);
        }
        QueryWrapper<CWeightSheets> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.isNull("WEISHT_LOAD_TM");
        queryWrapper1.isNotNull("WEISHT_EMPTY_TM");
        queryWrapper1.eq("WEISHT_DELETED", 0);
        queryWrapper1.eq("WEISHT_BACKED", 1);
        queryWrapper1.in("WEISHT_PRODUCT", pdtName);
        queryWrapper1.notIn("WEISHT_SEL_ASHGATE_ID",timeImproveByAshgate());
        List<CWeightSheets> cWeightSheetsList = cWeightSheetsService.list(queryWrapper1);
        for (CWeightSheets cWeightSheets : cWeightSheetsList) {
            UpdateWrapper<CWeightSheets> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("ID", cWeightSheets.getId());
            updateWrapper.set("WEISHT_LOAD_TM", null);
            cWeightSheetsService.update(updateWrapper);
        }
    }
    //    处理不需要装载结束时间的
    public void timeImprove7() {
        List<String> pdtName = new ArrayList<>();
        QueryWrapper<CProductsInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("PDTINF_CLASS", 4);
        List<CProductsInfo> productsInfoList = cProductsInfoService.list(queryWrapper);
        for (CProductsInfo cProductsInfo : productsInfoList) {
            String pdt = cProductsInfo.getPdtinfPdt();
                pdtName.add(pdt);
        }
        QueryWrapper<CWeightSheets> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.isNull("WEISHT_LOAD_END_TM");
        queryWrapper1.isNotNull("WEISHT_FULL_TM");
        queryWrapper1.eq("WEISHT_DELETED", 0);
        queryWrapper1.eq("WEISHT_BACKED", 1);
        queryWrapper1.notIn("WEISHT_SEL_ASHGATE_ID",timeImproveByAshgate());
        queryWrapper1.in("WEISHT_PRODUCT", pdtName);
        List<CWeightSheets> cWeightSheetsList = cWeightSheetsService.list(queryWrapper1);
        for (CWeightSheets cWeightSheets : cWeightSheetsList) {
            UpdateWrapper<CWeightSheets> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("ID", cWeightSheets.getId());
            updateWrapper.set("WEISHT_LOAD_END_TM", null);
            cWeightSheetsService.update(updateWrapper);
        }
    }
//    不需要恢复装灰开始和结束时间的装载点
    public List<Integer> timeImproveByAshgate() {
    List<Integer> ashgateId = new ArrayList<>();
    ashgateId.add(18);
    ashgateId.add(19);
    ashgateId.add(20);
        ashgateId.add(41);
        return  ashgateId;
    }
//    不需要恢复出门进门时间的
    public List<Integer> timeImproveByAshgate2() {
        List<Integer> ashgateId = new ArrayList<>();
        ashgateId.add(20);
        return  ashgateId;
    }
}
